package com.jinyang.arouterdemo

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.databinding.DataBindingUtil
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.facade.callback.NavigationCallback
import com.alibaba.android.arouter.facade.enums.RouteType
import com.alibaba.android.arouter.facade.model.RouteMeta
import com.alibaba.android.arouter.facade.template.IRouteGroup
import com.alibaba.android.arouter.launcher.ARouter
import com.jinyang.arouterdemo.databinding.ActivityMainBinding
import com.jinyang.baserouter.*
import com.jinyang.baserouter.model.TestObj
import com.jinyang.baserouter.model.TestParcelable
import com.jinyang.baserouter.model.TestSerializable

//import com.jinyang.login.RegisterActivity
//import com.jinyang.login.TestDialogFragment

@Route(path = PATH_ACTIVITY_MAIN)
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding =
            DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
        binding.btn2login.setOnClickListener {
            val testObj = TestObj("ljy", 18, "xxx@qq.com")
            val testObj2 = TestObj("yang", 20, "www@qq.com")
            val list = listOf(testObj, testObj2)
            val testSerializable = TestSerializable("serial", 12)
            val testParcelable = TestParcelable("parcel", 11)
            routerNavigation(
                PATH_ACTIVITY_LOGIN,
                mapOf(
                    "key1" to 123,
                    "key2" to "aaa",
                    "key3" to "bbb",
                    "testObj" to testObj,
                    "list" to list,
                    "testSerializable" to testSerializable,
                    "testParcelable" to testParcelable
                ),
                this@MainActivity,
                1001
            )
        }
        binding.btn2uri.setOnClickListener {
            routerNavigation( uri = Uri.parse("arouter://test.ljy.com$PATH_ACTIVITY_LOGIN"))
        }
        binding.btn2web.setOnClickListener {
            routerNavigation(
                PATH_ACTIVITY_WEB,
                mapOf("url" to "file:///android_asset/scheme-test.html"),
                group = "web1"
            )
        }
        ARouter.getInstance().addRouteGroup {
            it[PATH_ACTIVITY_REGISTER] = RouteMeta.build(
                RouteType.ACTIVITY,// 路由信息
                Class.forName("com.jinyang.login.RegisterActivity"),// 目标的 Class
                PATH_ACTIVITY_REGISTER, // Path
                PATH_ACTIVITY_REGISTER.split("/")[1],// Group, 尽量保持和 path 的第一段相同
                0, // 优先级，暂未使用
                0// Extra，用于给页面打标
            )
        }

        binding.btnAddRouteGroup.setOnClickListener {
            routerNavigation(PATH_ACTIVITY_REGISTER)
            routerNavigation(PATH_ACTIVITY_REGISTER)
        }

        binding.btnWithFlag.setOnClickListener {
            routerNavigation(PATH_ACTIVITY_REGISTER, flag = Intent.FLAG_ACTIVITY_CLEAR_TOP)
            routerNavigation(PATH_ACTIVITY_REGISTER, flag = Intent.FLAG_ACTIVITY_CLEAR_TOP)
            routerNavigation(PATH_ACTIVITY_REGISTER, flag = Intent.FLAG_ACTIVITY_CLEAR_TOP)
            routerNavigation(PATH_ACTIVITY_REGISTER, flag = Intent.FLAG_ACTIVITY_CLEAR_TOP)
        }


        binding.btnFailNav.setOnClickListener {
            routerNavigation("/aaa/bbb", callback = object : NavigationCallback {
                override fun onFound(postcard: Postcard?) {
                    log("NavigationCallback.onFound-找到了:$postcard")
                }

                override fun onLost(postcard: Postcard?) {
                    log("NavigationCallback.onLost-找不到了:$postcard")
                    routerNavigation(PATH_ACTIVITY_ERROR)
                }

                override fun onArrival(postcard: Postcard?) {
                    log("NavigationCallback.onArrival-跳转完了:$postcard")
                }

                override fun onInterrupt(postcard: Postcard?) {
                    log("NavigationCallback.onInterrupt-被拦截了:$postcard")
                }
            })
        }
        binding.btnFailNav2.setOnClickListener {
            routerNavigation("/aaa/bbb")
        }

        binding.btnHelloService.setOnClickListener {
            routerNavigation(PATH_ACTIVITY_HELLO_SERVICE)
        }

        binding.btnFragment.setOnClickListener {
            // 获取Fragment
            val fragment: AppCompatDialogFragment =
                ARouter.getInstance().build(PATH_FRAGMENT_DIALOG_TEST)
                    .navigation() as AppCompatDialogFragment
//            (fragment as TestDialogFragment).setConfirmCallback { log("MainActivity confirmCallback ") }
            fragment.show(supportFragmentManager, "TestDialogFragment")

        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        log("requestCode:$requestCode, resultCode:$resultCode, data:$data")
    }

}